﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ScannerApp.ScannerWebserviceWrapper;
using System.Text.RegularExpressions;

using ScannerApp.Utils;
using CodeBetter.Json;
using System.Collections;

namespace ScannerApp.Forms
{

    public partial class BindingQRForm : ScannerBaseForm
    {
        BindingQRResult mOrder;
        private string currentBarCode = String.Empty;
        private int currentOrderIndex = 1;
        private int currentScanIndex = 1;

        public BindingQRForm()
        {
            InitializeComponent();

 
        }

        public override void Init()
        {
            base.Init();
            this.mCurrentStatus = ScanBarcodeStatus.ShelfCode;
            this.mCurrentProductBarcodeType = ProductBarcodeType.Unknown;
            this.DispalyMessage("请扫描货位号");

        }
        public override void AssignControls()
        {
            this.lblMessage = this.labTitle;
            this.txtScanCode = this.txtScan;
            this.lblProductInfo = this.lblScanedInfo;
            this.lblHeaderInfo = this.lblHeader;
        }


        public override void HandleBarCode(string barCode)
        {
            try
            {
                base.HandleBarCode(barCode);

                currentBarCode = barCode;

                if (barCode.Equals(string.Empty))
                {
                    return;
                }

                if (mCurrentStatus == ScanBarcodeStatus.ShelfCode)
                {
                    ScanShelfCode(barCode);
                }
                else if (mCurrentStatus == ScanBarcodeStatus.ProductCode)
                {
                    ScanProductCode(barCode,this.mOrder);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void ScanShelfCode(string barCode)
        {
            BindingQRGetProductsParameter param = new BindingQRGetProductsParameter();
            param.ShelfCode = barCode;
            param.searchType = 1;
            param.currentPage = this.currentOrderIndex;
            param.currentScanPage = this.currentScanIndex;

            string strResult = ScannerWebServiceProxy.Instance.BindingQRGetProductsByShelf(Converter.Serialize(param));
            BaseResult<BindingQRResult> result = Converter.Deserialize<BaseResult<BindingQRResult>>(strResult);
            if (result.IsSucessful())
            {
                this.mCurrentShelfCode = barCode;
                this.mCurrentStatus = ScanBarcodeStatus.ProductCode;
                this.mOrder = result.ReturnValue;
                BindData(result.ReturnValue, "已扫描此货位，请扫描产品码");
            }
            else
            {
                DispalyMessage(result.getReturnCodeMessage());
            }
        }


        protected override void ScanQRCode(string QRcode)
        {
            Regex reg = new Regex("^[0-9a-zA-Z]+$");
            if (QRcode.Length < 15 || QRcode.Length > 20 || !reg.IsMatch(QRcode))
            {
                DispalyMessage("扫描序列号格式不正确.");
                return;
            }

            this.txtScan.Text = QRcode;

            
            if (this.mOrder.IsRepeatedScan(QRcode))
            {
                DispalyMessage("此码已经被扫描过了");
                return;
            }

            this.mCurrentProductQR = QRcode;
            this.DispalyProductMessage();

            BindingQRSingleProductParameter param = new BindingQRSingleProductParameter();
            param.ShelfCode = this.mCurrentShelfCode;
            param.ProductCode = QRcode;
            param.InventoryCode = this.mCurrentProductCode;
            param.Lot = this.mCurrentProductLot;
            param.Quantity = 1;


            string strResult = ScannerWebServiceProxy.Instance.BindingQRSingleProduct(Converter.Serialize(param));
            BaseResult<BindingQRResult> result = Converter.Deserialize<BaseResult<BindingQRResult>>(strResult);
            if (result.IsSucessful())
            {
                BindData(result.ReturnValue, "产品已绑定序列号.");
            }
            else
            {
                DispalyMessage(result.getReturnCodeMessage());
            }
          
        }

        private void BindData(BindingQRResult order, string message)
        {
            this.mScanProductOrder = order;
            this.dgOrder.TableStyles.Clear();
            this.dgOrder.TableStyles.Add(Utils.Utils.GetTableStyle(typeof(MergedBindingQRProductInfo)));

            RefreshOrderGrid(Utils.Utils.ConvertListToDataTable(order.batStockInfoList), order.OrderPageIndex
                     , order.OrderTotalPage, new BaseParameter().pageSize, order.OrderTotalCount);

            this.dgScan.TableStyles.Clear();
            this.dgScan.TableStyles.Add(Utils.Utils.GetTableStyle(typeof(QRStockInfo)));

            RefreshScanGrid(Utils.Utils.ConvertListToDataTable(order.qrStockInfoList), order.ScanPageIndex
                         , order.ScanTotalPage, new BaseParameter().pageSize, order.ScanTotalCount);

            DispalyMessage(message);
        }


        private void txtScan_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.HandleKeyPress(e);
        }

        private void RefreshOrderGrid(DataTable dataTable, int pageIndex, int totalPage, int pageSize, int totalCount)
        {
            this.dgOrder.DataSource = dataTable;
            if (this.currentOrderIndex == 1)
            {
                this.btnPrev.Enabled = false;
            }
            else
            {
                this.btnPrev.Enabled = true;
            }

            if (this.currentOrderIndex == totalPage)
            {
                this.btnNext.Enabled = false;
            }
            else
            {
                this.btnNext.Enabled = true;
            }
            this.lbOrderPagination.Text = "第" + pageIndex + "/" + totalPage + "页," + "每页" + pageSize + "条," + "共" + totalCount + "条";
            this.btnPrev.Visible = true;
            this.btnNext.Visible = true;
        }

        private void RefreshScanGrid(DataTable dataTable, int pageIndex, int totalPage, int pageSize, int totalCount)
        {
            this.dgScan.DataSource = dataTable;

            if (this.currentScanIndex == 1)
            {
                this.btnScanPrev.Enabled = false;
            }
            else
            {
                this.btnScanPrev.Enabled = true;
            }

            if (this.currentScanIndex == totalPage)
            {
                this.btnScanNext.Enabled = false;
            }
            else
            {
                this.btnScanNext.Enabled = true;
            }
            this.lbScanPagination.Text = "第" + pageIndex + "/" + totalPage + "页," + "每页" + pageSize + "条," + "共" + totalCount + "条";
            this.btnScanPrev.Visible = true;
            this.btnScanNext.Visible = true;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.currentOrderIndex++;
            batOrderPagination();

        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            this.currentOrderIndex--;
            batOrderPagination();
        }

        private void btnScanPrev_Click(object sender, EventArgs e)
        {
            this.currentScanIndex--;
            qrListScannerPagination();

        }

        private void btnScanNext_Click(object sender, EventArgs e)
        {
            this.currentScanIndex++;
            qrListScannerPagination();

        }

        private void batOrderPagination()
        {
            try
            {
                BindingQRGetProductsParameter param = new BindingQRGetProductsParameter();
                param.ShelfCode = this.mCurrentShelfCode;
                param.searchType = 2;
                param.currentPage = this.currentOrderIndex;


                string strResult = ScannerWebServiceProxy.Instance.BindingQRGetProductsByShelf(Converter.Serialize(param));
                BaseResult<BindingQRResult> result = Converter.Deserialize<BaseResult<BindingQRResult>>(strResult);
                if (result.IsSucessful())
                {
                    this.currentOrderIndex = result.ReturnValue.OrderPageIndex;

                    RefreshOrderGrid(Utils.Utils.ConvertListToDataTable(result.ReturnValue.batStockInfoList), result.ReturnValue.OrderPageIndex
                     , result.ReturnValue.OrderTotalPage, param.pageSize, result.ReturnValue.OrderTotalCount);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void qrListScannerPagination()
        {
            try
            {
                try
                {
                    BindingQRGetProductsParameter param = new BindingQRGetProductsParameter();
                    param.ShelfCode = this.mCurrentShelfCode;
                    param.searchType = 3;
                    param.currentScanPage = this.currentScanIndex;

                   string strResult = ScannerWebServiceProxy.Instance.BindingQRGetProductsByShelf(Converter.Serialize(param));
                    BaseResult<BindingQRResult> result = Converter.Deserialize<BaseResult<BindingQRResult>>(strResult);
                    if (result.IsSucessful())
                    {
                        this.currentScanIndex = result.ReturnValue.ScanPageIndex;

                        RefreshScanGrid(Utils.Utils.ConvertListToDataTable(result.ReturnValue.qrStockInfoList), result.ReturnValue.ScanPageIndex
                         , result.ReturnValue.ScanTotalPage, param.pageSize, result.ReturnValue.ScanTotalCount); 

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}